<template>
  <div class="app-container">
    <h2>总计:</h2>
    <el-table v-loading="countLoading" :data="countData" border stripe style="width: 100%;margin-top: 10px;">
      <el-table-column prop="maxScore" label="最高分"> </el-table-column>
      <el-table-column prop="minScore" label="最低分"> </el-table-column>
      <el-table-column prop="totalAvgScore" label="平均分"> </el-table-column>
      <el-table-column prop="goodNum" label="好评数"> </el-table-column>
      <el-table-column prop="badNum" label="差评数"> </el-table-column>
      <el-table-column prop="totalNum" label="评分总次数"> </el-table-column>
      <el-table-column prop="patientNum" label="患者人数"> </el-table-column>
      <el-table-column prop="interactNum" label="互动量"> </el-table-column>
    </el-table>
    <h2>详细数据:</h2>
    <basics-form :minWidth="'300px'" ref="basicsForm" :actionLoading="loading" @actionBtnsClick="actionBtnsClick"
      @submit="search" />
    <el-table v-loading="tableLoading" :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="deptName" label="分数">
          <template slot-scope="scope">
            {{
              scope.row.isGiveScore?scope.row.starsNum*scope.row.eachStarsScore :''
            }}
          </template>
      </el-table-column>
      <el-table-column prop="sendTime" label="时间"> </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="$refs.basicsForm.formValid()" />
  </div>
</template>

<script>
  import BasicsForm from "@/components/BasicsForm";
  import {
    countEvaluateOfWeb,patientAllList,evaluateListOfWeb
  } from "@/api/manbing/doctor";
  export default {
    name: "evaluate",
    components: {
      BasicsForm
    },
    data() {
      return {
        loading: false,
        tableLoading: false,
        total: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        countLoading: false,
        tableData: [],
        countData: [],
      };
    },
    created() {

    },
    methods: {
      init(doctorId) {
        this.tableData=[]
        this.countData=[]
        this.queryParams.pageNum=1
        this.queryParams.doctorId= doctorId
        this.getCountEvaluateOfWeb()
        this.getPatientAllList()
      },
      //患者对医生的评价-统计
      getCountEvaluateOfWeb() {
        this.countData = []
        this.countLoading = true
        countEvaluateOfWeb({
          doctorId: this.queryParams.doctorId
        }).then(res => {
          if (res.code == 200&&res.data) {
            this.countData.push(res.data)
          }
        }).finally(() => {
          this.countLoading = false;
        });
      },
      getPatientAllList(){
        this.patients=[]
        patientAllList({
          doctorId: this.queryParams.doctorId
        }).then(res => {
          if (res.code == 200) {
            this.initRules(res.data)
          }
        })
      },
      //顶部搜索组件
      initRules(data) {
        let rules = [
          {
            title:'患者',
            key:'patientId',
            type: "select",
            selectList: data,
            selectKey:'patientId',
          },
          {
            type: "actionBtns",
            selectList: [{
                name: "搜索",
                value: "primary"
              },
            ],
            ruleWidth: "auto",
          },
        ];
        this.$refs.basicsForm.initForm(rules);
      },
      //顶部按钮点击
      actionBtnsClick() {
        this.queryParams.pageNum = 1;
        this.$refs.basicsForm.formValid();
      },
      //搜索
      search(param) {
        if (!param) {
          return;
        }
        console.log(param,'----')
        for (let key in param) {
          this.queryParams[key] = param[key];
        }
        this.getList();
      },
      //列表数据
      getList() {
        this.loading = true;
        this.tableLoading = true;
        evaluateListOfWeb(this.queryParams)
          .then((res) => {
            if (res.code == 200) {
              this.tableData = res.rows;
              this.total = res.total;
            }
          })
          .finally(() => {
            this.loading = false;
            this.tableLoading = false;
          });
      },
      //详情和编辑
      showDetails(item) {
        this.openView = true;
        this.item = item;
        this.$nextTick(() => {
          this.getProvinces();
        });
      },
      //省市区列表
      getProvinces() {
        if (this.provinces) {
          this.getLevels();
        } else {
          provinceList().then((res) => {
            if (res.code == 200) {
              this.provinces = res.data;
              this.getLevels();
            }
          });
        }
      },
      //医院等级列表
      getLevels() {
        if (this.levels) {
          this.initAddRules();
        } else {
          listHospitalLevel().then((res) => {
            if (res.code == 200) {
              this.levels = res.data;
              this.initAddRules();
            }
          });
        }
      },
      //构建新增/修改表单
      initAddRules() {
        let item = this.item ? this.item : {};
        let rules = [{
            title: "名称",
            key: "deptName",
            value: item.deptName || "",
          },
          {
            title: "级别",
            key: "ds",
            type: "select",
            selectList: this.levels,
            value: item.level ? this.levels.indexOf(item.level) : "",
            skip: true,
          },
          {
            title: "省市区",
            key: "ssq",
            type: "cascader",
            cascaderProps: {
              children: "childs",
              label: "name",
              value: "name",
            },
            selectList: this.provinces,
            value: item.provinceName ?
              [item.provinceName, item.cityName, item.areaName] :
              [],
            skip: true,
          },
          {
            title: "详细地址",
            key: "address",
            type: "textarea",
            value: item.address || "",
            skip: true,
          },
          {
            title: "管理员姓名",
            key: "leader",
            value: item.leader || "",
          },
          {
            title: "管理员手机号(账号)",
            key: "phone",
            maxlength: 11,
            ruleList: ["isMobile"],
            ruleMsgList: ["请输入正确的手机号码"],
            value: item.phone || "",
          },
          {
            title: "管理员登录密码",
            key: "pwd",
            ruleList: ["minLength:6"],
            ruleMsgList: ["登录密码至少6位"],
            hidden: item.deptId ? true : false,
          },
          {
            type: "actionBtns",
            selectList: [{
                name: "取消",
                value: ""
              },
              {
                name: "提交",
                value: "primary"
              },
            ],
            btnsAligin: "center",
            ruleWidth: "503px",
          },
        ];
        this.$refs.basicsAddForm.initForm(rules);
      },
      //新增表单的按钮点击事件
      addAction(index) {
        if (index) {
          this.$refs.basicsAddForm.formValid();
        } else {
          this.openView = false;
        }
      },
      //新增，发送请求
      addSubmit(param) {
        if (!param) {
          return;
        }
        param.provinceName = param.ssq[0];
        param.cityName = param.ssq[1];
        param.areaName = param.ssq[2];
        delete param.ssq;
        if (this.item) {
          param.deptId = this.item.deptId;
          delete param.pwd;
        }
        this.loading = true;
        saveHospital(param)
          .then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess(this.item ? "修改成功" : "新增成功");
              this.openView = false;
              this.getList();
            }
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //删除
      del(deptId) {
        this.loading = true;
        this.tableLoading = true;
        delHospital({
            deptId
          })
          .then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.getList();
            }
          })
          .finally(() => {
            this.loading = false;
            this.tableLoading = false;
          });
      },
      //重置密码
      setPwd(deptId) {
        this.$prompt("请输入新密码", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPattern: /^\S{6,}$/,
          inputErrorMessage: "密码至少6位",
        }).then(({
          value
        }) => {
          this.loading = true;
          this.tableLoading = true;
          resetPwdForHospital({
              deptId,
              pwd: value
            })
            .then((res) => {
              if (res.code == 200) {
                this.$modal.msgSuccess("重置密码成功");
              }
            })
            .finally(() => {
              this.loading = false;
              this.tableLoading = false;
            });
        });
      },
    },
  };
</script>
<style scoped lang="scss">
</style>
